import { createStore } from 'vuex'
import getters from './getters'
export default createStore({
  state: {
    cartList: []
  },
  mutations: {
    addCount(state, payload) {
      payload.count++
    },
    addToCart(state, payload) {
      state.cartList.push(payload)
    }
  },
  getters,
  actions: {
    addCart(context, payload) {
      console.log(payload);
      let oldProduct = context.state.cartList.find(item => item.iid === payload.iid)
      //商品数量的加减
      if (oldProduct) {
        context.commit('addCount', oldProduct)
      } else {
        payload.count = 1
        context.commit('addToCart', payload)
      }
    }
  },
  modules: {
  }
})
